package com.ruoyi.framework.task;

import com.ruoyi.common.utils.BasicCarUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.data.domain.Car;
import com.ruoyi.project.data.domain.TbTransportDriverCar;
import com.ruoyi.project.data.service.ITbTransportDriverCarService;
import com.ruoyi.project.data.service.impl.AbnormalServiceImpl;
import com.ruoyi.project.data.service.impl.TbTransportDriverCarServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.utils.BasicCarUtils.*;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    private static final Logger logger = LoggerFactory.getLogger(RyTask.class);

    @Autowired
    private TbTransportDriverCarServiceImpl driverCarServiceImpl;
    @Autowired
    private ITbTransportDriverCarService tbTransportDriverCarService;
    @Autowired
    private AbnormalServiceImpl abnormalServiceImpl;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }


    /**
     * 定时爬取最新批次车辆信息
     */
    public void getNewBatchCar(){

        System.out.println("------------------开始执行定时任务getNewBatchCar-------------------");
        logger.info("开始执行定时任务getNewBatchCar");

        try{

            String newBatch = driverCarServiceImpl.getNewBatch();
            if (StringUtils.isNotBlank(newBatch)){
                String batch = getBatch();
                if (StringUtils.isNotBlank(batch)){
                    if (Integer.parseInt(batch) > Integer.parseInt(newBatch)){
                        Integer pageNum = getPageNum(batch);
                        for (int i=1; i<=pageNum;i++){
                            String batchHtml = HttpUtils.sendGet("https://www.cn357.com/notice_"+batch+"_"+i, "");
                            Document document = Jsoup.parse(batchHtml);
                            List<String> strings = document.getElementsByClass("listTable").first().getElementsByTag("a").eachAttr("href");
                            if (strings != null && strings.size() >0){
                                for (String s : strings){
                                    Car car = getCar(s);
                                    driverCarServiceImpl.insertCar(car);
                                }
                            }
                        }
                    }else {
                    logger.info("暂无最新批次车辆");
                    System.out.println("------------------暂无最新批次车辆-------------------");
                }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            logger.error("定时任务getNewBatchCar执行失败:"+e.getMessage());
            System.out.println("------------------定时任务getNewBatchCar执行失败-------------------");
        }


        logger.info("定时任务getNewBatchCar执行结束");
        System.out.println("------------------定时任务getNewBatchCar执行结束-------------------");
    }

    /**
     * 车辆库
     */
    public void updateCar() {

        System.out.println("------------------开始执行定时任务updateCar-------------------");

        try {
            // 插入标准车
            TbTransportDriverCar tbTransportDriverCar = new TbTransportDriverCar();
            tbTransportDriverCar.setSource("1");
            List<TbTransportDriverCar> tbTransportDriverCars = tbTransportDriverCarService.selectTbTransportDriverCarList(tbTransportDriverCar);
            if (tbTransportDriverCars != null && tbTransportDriverCars.size() > 0) {
                for (TbTransportDriverCar car : tbTransportDriverCars) {
                    car.setSource("2");

                    String carModel2 = tbTransportDriverCarService.getModel(car.getUserIdCard(), car.getCarLicenceNumber());
                    String num = "";
                    // 获取轴数
                    if (StringUtils.isNotBlank(carModel2)) {
                        String model = BasicCarUtils.getRealModel(carModel2);
                        Car car1 = tbTransportDriverCarService.getCar(model);
                        if (car1 == null) {
                            car1 = BasicCarUtils.getBasicCar(model);
                            if (car1 != null) {
                                if (isNumeric(car1.getAxleNumber())) {
                                    num = car1.getAxleNumber();
                                } else {
                                    if (car1.getAxleNumber().contains("线")){
                                        String substring = car1.getAxleNumber().substring(car1.getAxleNumber().indexOf("线") - 1, car1.getAxleNumber().indexOf("线"));
                                        if (isNumeric(substring)) {
                                            num = substring;
                                        } else {
                                            num = String.valueOf(chineseNumber2Int(substring));
                                        }
                                    }else {
                                        String substring = car1.getAxleNumber().substring(car1.getAxleNumber().indexOf("轴") - 1, car1.getAxleNumber().indexOf("轴"));
                                        if (isNumeric(substring)) {
                                            num = substring;
                                        } else {
                                            num = String.valueOf(chineseNumber2Int(substring));
                                        }
                                    }
                                }
                            }else {
                                num = car.getCarAxleNums();
                                car.setSource("1");
                            }
                        }else {
                            if (isNumeric(car1.getAxleNumber())) {
                                num = car1.getAxleNumber();
                            } else {
                                if (car1.getAxleNumber().contains("线")){
                                    String substring = car1.getAxleNumber().substring(car1.getAxleNumber().indexOf("线") - 1, car1.getAxleNumber().indexOf("线"));
                                    if (isNumeric(substring)) {
                                        num = substring;
                                    } else {
                                        num = String.valueOf(chineseNumber2Int(substring));
                                    }
                                }else {
                                    String substring = car1.getAxleNumber().substring(car1.getAxleNumber().indexOf("轴") - 1, car1.getAxleNumber().indexOf("轴"));
                                    if (isNumeric(substring)) {
                                        num = substring;
                                    } else {
                                        num = String.valueOf(chineseNumber2Int(substring));
                                    }
                                }
                            }
                        }
                    }

                    if (StringUtils.isNotBlank(num)){
                        car.setCarAxleNums(num);
                        driverCarServiceImpl.updateCar(car);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("------------------定时任务updateCar执行失败,原因："+e.getMessage());
        }
        System.out.println("------------------定时任务updateCar执行结束-------------------");
    }

    public void updateWeightRecord() {

        System.out.println("------------------开始执行定时任务updateWeightRecord-------------------");

        try {
            Map<String, Object> param = new HashMap<>();
            param.put("isUpdate","0");
            abnormalServiceImpl.updateRecord(param);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("------------------定时任务updateWeightRecord执行失败,原因："+e.getMessage());
        }
        System.out.println("------------------定时任务updateWeightRecord执行结束-------------------");
    }

    public static String  standardQuality(String qulity){

        if (qulity.contains("--")){
            return qulity.split("--")[1];
        }
        if (qulity.contains("吨")){
            return qulity.split("吨")[0] +"000";
        }
        if (qulity.contains("kg")){
            return qulity.split("kg")[0];
        }
        if (qulity.contains("KG")){
            return qulity.split("KG")[0];
        }
        if (qulity.contains("K")){
            return qulity.split("K")[0];
        }
        if (qulity.contains("Kg")){
            return qulity.split("Kg")[0];
        }
        if (qulity.contains("k")){
            return qulity.split("k")[0];
        }
        if (qulity.contains("T")){
            return qulity.split("T")[0];
        }
        if (qulity.contains(".")){
            return qulity.replace(".","");
        }
        return qulity;
    }
}
